using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Configuration;


namespace libEOD
{
    public class Web2File
    {
        HttpWebRequest      m_webRequest = null;
        HttpWebResponse     m_webResponse = null;
        Stream              m_Stream = null;
        WebProxy            m_Proxy = null;
        private DateTime    m_dtDate = DateTime.Today;
        string m_sTempFile = string.Empty;

        public string FileName { get { return m_sTempFile; } }


        public string GetPage(string sURL)
        {
            DateTime dtNow = DateTime.Now;
            FileStream fs = null;
            try
            {
                //fnCleanUp();
                m_webRequest = (HttpWebRequest)WebRequest.Create(sURL);
                //TODO add config proxy 
                //string proxyurl = "http://172.21.151.31:3132/accelerated_pac_base.pac";
                //m_Proxy = new WebProxy(proxyurl, true);
                //m_webRequest.Proxy = m_Proxy;
                m_webRequest.Headers.Add("Cache-Control", "no-cache");// 
                m_webRequest.Timeout = 30000;
                m_webResponse = (HttpWebResponse)m_webRequest.GetResponse();
                m_Stream = m_webResponse.GetResponseStream();
                m_sTempFile = Path.GetTempFileName();
                fs = new FileStream(m_sTempFile, FileMode.Create, FileAccess.ReadWrite);
                fnReadWriteStream(m_Stream, fs);
                TimeSpan ts = (TimeSpan)(DateTime.Now - dtNow);
                //TODO Add Loggin Global.Info("WSJ: Web Request completed in {0} sec {1} msec", ts.Seconds,ts.Milliseconds);
                return "OK";
            }
            catch (WebException we)
            {
                TimeSpan ts = (TimeSpan)(DateTime.Now - dtNow);
                //Global.Info("WSJ: Web Request completed in {0} sec {1} msec", ts.Seconds, ts.Milliseconds);
                //Global.Info(we.ToString());
                //Global.Info(we.Message);
                //fnCleanUp();
                return "TIMEDOUT";
            }
            catch (Exception e)
            {
                TimeSpan ts = (TimeSpan)(DateTime.Now - dtNow);
                //Global.Info("WSJ: Web Request completed in {0} sec {1} msec", ts.Seconds, ts.Milliseconds);
                //Global.Error(e.ToString());
                //fnCleanUp();
                return e.ToString();
            }
        }
        private void fnReadWriteStream(Stream readStream, Stream writeStream)
        {
            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = readStream.Read(buffer, 0, Length);
            while (bytesRead > 0)
            {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = readStream.Read(buffer, 0, Length);
            }
            readStream.Close();
            writeStream.Close();
        }
    }
}
